<template>
  <div>运动管理
    <el-button style="margin-left: 1180px;width:  100px" type="primary"  @click="dialogFormVisible1 = true">添加</el-button>
    <el-dialog v-model="dialogFormVisible1" title="添加">
      <el-form :model="form">
        <el-form-item label="运动名" >
          <el-input v-model="form.name"  />
        </el-form-item>
        <el-form-item label="消耗" >
          <el-input v-model="form.heat"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取消</el-button>
        <el-button type="primary" @click="add();dialogFormVisible1 = false;load()">
          添加
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
  <el-divider></el-divider>
  <el-table :data="form.tableData" style="width: 100%">
    <el-table-column label="ID" prop="id" />
    <el-table-column label="名称" prop="name" />
    <el-table-column label="kcal/h" prop="heat" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="搜索" />
      </template>
      <template #default="scope">
        <el-button size="small" type="success" style="margin-right: 20px"  @click="dialogFormVisible2 = true;save(scope.row.id,scope.row.name, scope.row.heat)" plain>
          编辑
        </el-button>
        <el-dialog v-model="dialogFormVisible2" title="修改">
          <el-form :model="form">
            <el-form-item label="运动名" >
              <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="kcal/h" >
              <el-input v-model="form.heat" autocomplete="off" />
            </el-form-item>
          </el-form>
          <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取消修改</el-button>
        <el-button type="primary" @click="update();dialogFormVisible2 = false;load()">
          确定
        </el-button>
      </span>
          </template>
        </el-dialog>
        <el-popconfirm
            title="确定删除吗？"
            confirmButtonText='确定'
            cancelButtonText='取消'
            size="small"
            type="danger"
            @confirm="save(scope.row.id,scope.row.name,scope.row.heat);deletesport();load()"
            plain
        ><template #reference>
          <el-button style="cursor: pointer"  type="danger"  plain >删除</el-button>
        </template></el-popconfirm
        >
      </template>
    </el-table-column>
  </el-table>

</template>

<script lang="ts" setup>
import {computed, onMounted, reactive, ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from "axios";
import {post} from '../net';
import {getsportList} from "../net";
import {deleteSport} from "../net";
const dialogFormVisible1 = ref(false)
const dialogFormVisible2 = ref(false)
const form = reactive({
  id:'',
  name: '',
  heat:'',
  tableData: [],
  oldName: '',
  oldHeat: ''
})
const save = (id,name, heat) => {
  form.id=id
  form.oldName=name
  form.oldHeat=heat
}
const search = ref('')
const load=async ()=>{
  let data = (await getsportList()).data
  form.tableData=data
}
onMounted( ()=>{
  load()
})
const add = ()=>{
  if(!form.name&&form.heat)
  { console.log("555")
    ElMessage.warning('请填写运动信息')}
  else {
    post('/sport/add', {
      name:form.name,
      heat:form.heat,
    }, (message)=>{
      ElMessage.success("添加成功")
    })
    ElMessage.success("添加成功")
  }
  load()
}
const update =()=> {
  if(!form.name&&!form.heat)
  { console.log("555")
    ElMessage.warning('请填写运动信息')}
  else {
    if(form.name==null){
      post('/sport/update', {
        id:form.id,
        name:form.oldName,
        heat:form.heat,
      },{})
    }
    else if(form.heat==null){
      post('/sport/update', {
        id:form.id,
        name:form.name,
        heat:form.oldHeat,
      },{})
    }
    else {
      post('/sport/update', {
        id:form.id,
        name:form.name,
        heat:form.heat,
      },{})
    }

    ElMessage.success('修改成功')
    load()
  }
}
const deletesport =()=> {
  console.log(form.oldName)
  post('/sport/delete', {
    name:form.oldName,
  },{})
  ElMessage.success('删除成功')
  load()
}

</script>

<style scoped>

</style>
